package com.sxt;

import java.awt.*;

public class Line {
    //起点坐标
    int x = 380;
    int y = 180;
    //终点坐标
    int endX = 500;
    int endY = 500;
    //线长
    double length = 100;
    double MinLength = 50;
    double MaxLength = 800;

    double n = 0;
    //方向
    int dir = 1;
    //状态 0 摇摆  1 抓取  2 收回
    int state;
    //狗爪
    Image hook = Toolkit.getDefaultToolkit().getImage("imgs/hook.png");

    GameWin frame;
    Line(GameWin frame){
        this.frame = frame;
    }
    //碰撞检测  检测是否抓取到物体
    void logic(){
        for (Object object : this.frame.objectList) {
            if (endX > object.x && endX < object.x + object.width
                    && endY > object.y && endY < object.y + object.height){
                state = 3;
                object.flag = true;
            }
        }
    }
    //绘制方法
    void lines(Graphics g){
        endX = (int) (x + length * Math.cos(n * Math.PI));
        endY = (int) (y + length * Math.sin(n * Math.PI));
        g.setColor(Color.RED);
        g.drawLine(x - 1,y,endX - 1,endY);
        g.drawLine(x,y,endX,endY);
        g.drawLine(x + 1,y,endX + 1,endY);
        g.drawImage(hook,endX - 36,endY - 2,null);
    }

    void paintSelf(Graphics g) {
        logic();
        switch (state){
            case 0:
                if (n<0.1){
                    dir = 1;
                }else if (n >0.9){
                    dir = -1;
                }
                n = n + 0.005 * dir;
                lines(g);
                break;
            case 1:
                if (length < MaxLength){
                    length = length + 10;
                    lines(g);
                }else {
                    state = 2;
                }
                break;
            case 2:
                if (length > MinLength){
                    length = length - 10;
                    lines(g);
                }else {
                    state = 0;
                }
                break;
            case 3:
                int m = 1;
                if (length > MinLength){
                    length = length - 10;
                    lines(g);
                    for (Object object : this.frame.objectList) {
                        if (object.flag){
                            m = object.m;
                            object.x = endX - object.getWidth() / 2;
                            object.y = endY;
                            if (length<=MinLength){
                                object.x = -150;
                                object.y = -150;
                                Bj.waterFlag = false;
                                //加分
                                Bj.count += object.count;
                                object.flag = false;
                                state = 0;
                            }
                            if (Bj.waterFlag){
                                if (object.type == 1){
                                    m =1;
                                }
                                if (object.type == 2){
                                    object.x = -150;
                                    object.y = -150;
                                    object.flag = false;
                                    Bj.waterFlag = false;
                                    state = 2;
                                }
                            }
                        }
                    }
                }
                try {
                    Thread.sleep(m);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            default:

        }


    }
    void reGame(){
        x = 0;
        length = 100;
    }
}
